package com.spa.interfaces.admin;

import cn.dev33.satoken.annotation.SaIgnore;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.*;
import com.spa.application.query.admin.OrderDetailQuery;
import com.spa.application.query.admin.OrderExportQuery;
import com.spa.application.query.admin.OrderQuery;
import com.spa.application.query.admin.TransferOrderPageQuery;
import com.spa.application.service.admin.AdminOrderService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.infrastructure.config.SysDictEnumConfig;
import com.spa.infrastructure.enums.dict.DictionaryData;
import com.spa.infrastructure.enums.dict.DictionaryManagement;
import com.spa.interfaces.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("admin/order")
public class AdminOrderController extends BaseController {

    @Autowired
    private AdminOrderService adminOrderService;

    @PostMapping("list")
    public ResponseMessage<PageResult<OrderListDTO>> list(@RequestBody OrderQuery query) {
        return responseHelper.success(adminOrderService.listOrder(query));
    }

    @PostMapping("writeList")
    public void writeList(HttpServletResponse response, @RequestBody OrderExportQuery query) throws Exception {
        query.setCurrent(1L);
        query.setSize(20000L);
        List<OrderExportDTO> records = adminOrderService.listExportOrder(query);
        exportQueryResult(response, records, "订单列表","订单列表",OrderExportDTO.class);
    }

    @PostMapping("detail")
    public ResponseMessage<OrderDetailDTO> detail(@RequestBody OrderDetailQuery query) {
        return responseHelper.success(adminOrderService.detailById(query));
    }

    @PostMapping("transfer/page")
    public ResponseMessage<PageResult<TransferOrderPageDTO>> transferPage(@RequestBody TransferOrderPageQuery query) {
        return responseHelper.success(adminOrderService.transferPage(query));
    }

    @PostMapping("transfer/getTransferOrderByOrderCode")
    public ResponseMessage<List<TransferOrderPageDTO>> getTransferOrderByOrderCode(@RequestBody TransferOrderPageQuery query) {
        return responseHelper.success(adminOrderService.getTransferOrderByOrderCode(query.getOrderCode()));
    }

    @PostMapping("transfer/export")
    public void exportTransferOrder(HttpServletResponse response,@RequestBody TransferOrderPageQuery query) throws Exception {
        List<TransferOrderExportDTO> records = adminOrderService.listExportTransferOrder(query);
        exportQueryResult(response, records, "流转订单列表","流转订单列表",TransferOrderExportDTO.class);
    }

    @PostMapping("/sysDictByType")
    @SaIgnore
    public ResponseMessage<OrderStatusDictionaryData> dictTypeList() {
        List<DictionaryManagement> dictList = SysDictEnumConfig.dictManageList;
        List<OrderStatusDictionaryData> result = new ArrayList<>();
        dictList.stream().forEach(a -> {
            if(a.getDictType().equals("orderStatus")){
                List<DictionaryData> ddList = a.getDdList();
                ddList.forEach(dictionaryData -> {
                    result.add(OrderStatusDictionaryData.builder()
                            .dictTag(dictionaryData.getDictTag())
                            .dictValue(dictionaryData.getDictValue())
                            .build());
                });
            }
        });
        return responseHelper.success(result);
    }

}
